<?php
class Library_Model_VanBanMapper extends Library_Model_DataMapperAbstract{
	private static $_instance = null;
	private $_paginator = null ;
	
	public function searchVanBanById($id)
	{
		$db = $this->getDb();
		$selectVanban = $db->select()
		->from('tbl_vanban')
		->where('MaVanBan = ?',$id);
		$vanban = $db->fetchRow($selectVanban);
		
		$loaivanbanMapper = Library_Model_LoaiVanBanMapper::getInstance();
		$loaivanban = $loaivanbanMapper->searchLoaiVanBanById($vanban['MaLoaiVanBan']);
		$vanban['tbl_loaivanban'] = $loaivanban;
		
		$chinhanhMapper = Library_Model_ChiNhanhMapper::getInstance();
		$chinhanh = $chinhanhMapper->searchChiNhanhById($vanban['MaChiNhanh']);
		$vanban['tbl_chinhanh'] = $chinhanh;
		
		$phongbanMapper = Library_Model_PhongBanMapper::getInstance();
		$phongban = $phongbanMapper->searchPhongBanById($vanban['MaPhongBan']);
		$vanban['tbl_phongban'] = $phongban;
		
		$nhomvanbanMapper = Library_Model_NhomVanBanMapper::getInstance();
		$nhom = $nhomvanbanMapper->searchNhomVanBanById($vanban['MaNhom']);
		$vanban['tbl_nhomvanban'] = $nhom;
		
		$taptinMapper = Library_Model_TapTinMapper::getInstance();
		$taptin = $taptinMapper->searchByMaVanBan($vanban['MaVanBan']);
		$vanban['tbl_taptin'] = $taptin;
	
		return $this->_populate($vanban);	
	}
	
	
	public function vanbanList()
	{
		$db = $this->getDb();
		$selectVanban = $db->select()
										->from('tbl_vanban');
		$vanbans = $db->fetchAll($selectVanban);
	
		$vanbanObjArray = array();
		foreach($vanbans as $vanban){
		$loaivanbanMapper = Library_Model_LoaiVanBanMapper::getInstance();
		$loaivanban = $loaivanbanMapper->searchLoaiVanBanById($vanban['MaLoaiVanBan']);
		$vanban['tbl_loaivanban'] = $loaivanban;
		
		$chinhanhMapper = Library_Model_ChiNhanhMapper::getInstance();
		$chinhanh = $chinhanhMapper->searchChiNhanhById($vanban['MaChiNhanh']);
		$vanban['tbl_chinhanh'] = $chinhanh;
		
		$phongbanMapper = Library_Model_PhongBanMapper::getInstance();
		$phongban = $phongbanMapper->searchPhongBanById($vanban['MaPhongBan']);
		$vanban['tbl_phongban'] = $phongban;
		
		$nhomvanbanMapper = Library_Model_NhomVanBanMapper::getInstance();
		$nhom = $nhomvanbanMapper->searchNhomVanBanById($vanban['MaNhom']);
		$vanban['tbl_nhomvanban'] = $nhom;
		
		$taptinMapper = Library_Model_TapTinMapper::getInstance();
		$taptin = $taptinMapper->searchByMaVanBan($vanban['MaVanBan']);
		$vanban['tbl_taptin'] = $taptin;
		
			$vanbanObjArray[] = $this->_populate($vanban);
		}
	
		return $vanbanObjArray;
	}
	
	public function _populate($data){
		$obj = new Library_Model_VanBan();
		$obj->setId($data['MaVanBan']);
		$obj->setMaloaivanban($data['MaLoaiVanBan']);
		$obj->setLoaivanban($data['tbl_loaivanban']);		
		$obj->setTenvanban($data['TenVanBan']);
		$obj->setNgay($data['Ngay']);
		$obj->setNgaybanhanh($data['NgayBanHanh']);
		$obj->setNgaycohieuluc($data['NgayCoHieuLuc']);
		$obj->setMachinhanh($data['MaChiNhanh']);
		$obj->setChinhanh($data['tbl_chinhanh']);
		$obj->setMaNhom($data['MaNhom']);
		$obj->setNhom($data['tbl_nhomvanban']);
		$obj->setMaPhongban($data['MaPhongBan']);
		$obj->setPhongban($data['tbl_phongban']);
		$obj->setTrangthai($data['TrangThai']);
		$obj->setDoquantrong($data['DoQuanTrong']);
		$obj->setNguoiduyet($data['NguoiDuyet']);
		$obj->setTrichdan($data['NoiDung']);
		$obj->setTaptin($data['tbl_taptin']);
		
		return $obj;
	}
	
	protected function _insert(Library_Model_DomainObjectAbstract $obj){
		$db = $this->getDb();
		$data =array(
				'MaVanBan' 		=> $obj->getId(),
				'MaLoaiVanBan'	=> $obj->getMaloaivanban(),
				'TenVanBan'			=> $obj->getTenvanban(),
				'MaNhom'			=>$obj->getManhom(),
				'Ngay'					=> $obj->getNgay(),
				'NgayBanHanh'	=> $obj->getNgaybanhanh(),
				'NgayCoHieuLuc'=> $obj->getNgaycohieuluc(),
				'MaPhongBan'		=> $obj->getMaphongban(),
				'MaChiNhanh'		=> $obj->getMachinhanh(),
				'TrangThai'			=> $obj->getTrangthai(),
				'DoQuanTrong' 	=> $obj->getDoquantrong(),
				'NguoiDuyet' 		=> $obj->getNguoiduyet(),
				'NoiDung'			=> $obj->getTrichdan(),
			//	'TenTapTin'				=> $obj->getTaptin()
		);
		$db->insert('tbl_vanban',$data);
	}
	protected function _update(Library_Model_DomainObjectAbstract $obj){
	
		$db = $this->getDb();
		$data =array(	
				'MaVanBan' 		=> $obj->getId(),
				'MaLoaiVanBan'	=> $obj->getMaloaivanban(),
				'TenVanBan'			=> $obj->getTenvanban(),
				'MaNhom'			=>$obj->getManhom(),
				'Ngay'					=> $obj->getNgay(),
				'NgayBanHanh'	=> $obj->getNgaybanhanh(),
				'NgayCoHieuLuc'=> $obj->getNgaycohieuluc(),
				'MaPhongBan'		=> $obj->getMaphongban(),
				'MaChiNhanh'		=> $obj->getMachinhanh(),
				'TrangThai'			=> $obj->getTrangthai(),
				'DoQuanTrong' 	=> $obj->getDoquantrong(),
				'NguoiDuyet' 		=> $obj->getNguoiduyet(),
				'NoiDung'			=> $obj->getTrichdan(),
		);
		$where = $db->quoteInto('MaVanBan = ?', $obj->getId());
		$db->update('tbl_vanban', $data, $where);
	}
	
	public function delete($id)
	{
		$db = $this->getDb();
		$where = $db->quoteInto('MaVanBan = ?',$id);
		$db->delete('tbl_vanban',$where);
	}
	public static function getInstance(){
		if(!isset(self::$_instance)){
			$c = __CLASS__;
			self::$_instance = new $c;
		}
		return self::$_instance;
	}
}